//
//  TBComposeViewController.swift
//  新浪微博
//
//  Created by bin on 16/2/3.
//  Copyright © 2016年 XMG. All rights reserved.
//

import UIKit

class TBComposeViewController: UIViewController {

    
    @IBOutlet weak var customTextView: TBComposeTextView!
    
    @IBOutlet weak var toolbarBottomCons: NSLayoutConstraint!
    
    @IBAction func picBtnClicked(sender: UIButton) {
        
    }
    
    @IBAction func emoticonBtnClicked(sender: UIButton) {
        customTextView.resignFirstResponder()
        customTextView.inputView = (customTextView.inputView == nil) ? inputVC.view : nil
        customTextView.becomeFirstResponder()
    }
    
    @IBAction func cancleClicked(sender: UIBarButtonItem) {
        dismissViewControllerAnimated(true, completion: nil)
    }

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        inputVC.delegate = self
        addChildViewController(inputVC)
        
        // 1.设置textView代理
        customTextView.delegate = self
        
        // 2.监听键盘变化
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillChange:"), name: UIKeyboardWillChangeFrameNotification, object: nil)
        
        navigationItem.rightBarButtonItem?.enabled = false
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 召唤键盘
        customTextView.becomeFirstResponder()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        // 注销键盘
        customTextView.resignFirstResponder()
    }
    
    deinit
    {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK: - 内部控制方法
    @objc private func keyboardWillChange(notify: NSNotification)
    {
        // 弹出: UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";
        // 关闭: UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";
        
        // 约束值 = 屏幕高度 - 键盘的Y
        //         736 - 465 = 271
        //         736 - 736 = 0
        // 1.取出键盘frame
        let rect = notify.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue
        // 2.计算偏移位
        let offsetY = UIScreen.mainScreen().bounds.height - rect.origin.y
        // 3.修改偏移位
        toolbarBottomCons.constant = offsetY
        let duration = notify.userInfo![UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
        UIView.animateWithDuration(duration) { () -> Void in
            UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: 7)!)
            self.view.layoutIfNeeded()
        }
    }
    
    // MARK: - 懒加载
    lazy var inputVC: TBInputViewController = TBInputViewController()
}

extension TBComposeViewController: UITextViewDelegate, TBInputViewDelegte
{
    func textViewDidChange(textView: UITextView) {
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        customTextView.resignFirstResponder()
    }
    
    func inputViewCellDidCliked(model: TBEmoticonModel) {
        customTextView.text = model.codeStr
    }
}

